package org.swan.web.role.controller;

import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.stream.base.MessageBean;
import org.swan.web.role.bean.RoleBean;
import org.swan.web.role.service.RoleService;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    private final RoleService roleService;

    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    @GetMapping("/get")
    public MessageBean<RoleBean> get(@RequestParam("roleId") long roleId) {
        return MessageBean.MSG_SUCCESS.setData(roleService.getRoleById(roleId));
    }

    @PostMapping("add")
    public MessageBean<RoleBean> add(@RequestBody RoleBean roleBean) {
        long roleId = roleService.addRole(roleBean);
        return MessageBean.MSG_SUCCESS.setData(roleService.getRoleById(roleBean.getRoleId()));
    }

    @PostMapping("update")
    public MessageBean<RoleBean> update(@RequestBody RoleBean roleBean) {
        return MessageBean.MSG_SUCCESS.setData(roleService.updateRole(roleBean));
    }

    @GetMapping("delete")
    public MessageBean<RoleBean> delete(@RequestParam("roleId") long roleId) {
        return MessageBean.MSG_SUCCESS.setData(roleService.deleteRoleById(roleId));
    }

    @GetMapping("list")
    public MessageBean<List<RoleBean>> list(@Param("offset") int offset, @Param("rows") int rows) {
        return MessageBean.MSG_SUCCESS.setData(roleService.list(offset, rows));
    }
}
